﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            dynamic p1 = new System.Dynamic.ExpandoObject();
            p1.aa = 12;
            p1.bb = 24;
            p1.sayHello = new Func<string, string>((a) => {
                return "hello" + a;
            });
            string id;
            Request.Cookies.TryGetValue("id", out id);
            ViewData["id"] = id;
            ViewData["p1"] = p1;
            return View();
            
        }
        public IActionResult F1(string? name)
        {
            Person p1 = new Person();
            p1.age = 12;
            p1.name = name;
            p1.描述 = "的风景快递费就多发";
            Response.Cookies.Append("id", "gaokai",new CookieOptions() {MaxAge=new TimeSpan(2,0,0) });
            Response.Cookies.Append("name", "123456", new CookieOptions() { MaxAge = new TimeSpan(2, 0, 0, 0) });
            TempData.Add("code", 7328);
            HttpContext.Session.SetString("id", "zhangwen");
            var sessionid= HttpContext.Session.GetString("id");


            return Json(sessionid);
            return Json(p1, new JsonSerializerOptions() { Encoder=JavaScriptEncoder.UnsafeRelaxedJsonEscaping});
        }
        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
        class Person
        {
            public int age { get; set; }
            public string name { get; set; }
            public string 描述 { get; set; }
        }
    }
}
